﻿package com.yy.manager {
    import com.fminutes.vinciLib.utils.ObjectPool;
    import com.yy.manager.base.IFloatingPanel;
    
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class FloatingPanelManager {

        private var _timer:Timer;
        private var _parentDic:Dictionary;
        private var _delayDic:Dictionary;
        private var _hideDic:Dictionary;
        private var _targetDic:Dictionary;
        private var _panelDic:Dictionary;
        private var _panelParentDic:Dictionary;
        private var _panelEventDic:Dictionary;
        private var _panelTimeDic:Dictionary;
        private var _panelInstanceDic:Dictionary;

        public function FloatingPanelManager(){
            this._panelDic = new Dictionary(true);
            this._panelEventDic = new Dictionary(true);
            this._panelParentDic = new Dictionary(true);
            this._panelTimeDic = new Dictionary(true);
            this._panelInstanceDic = new Dictionary(true);
            this._parentDic = new Dictionary();
            this._delayDic = new Dictionary();
            this._hideDic = new Dictionary();
            this._targetDic = new Dictionary();
            this._timer = new Timer(50);
            this._timer.addEventListener(TimerEvent.TIMER, this.onTimer);
            this._timer.start();
        }
        public function show(_arg1:Object, _arg2:DisplayObjectContainer, _arg3:Object=null, _arg4:int=1000):void{
            if (_arg2 != null){
                this._parentDic[_arg1] = _arg2;
                this._delayDic[_arg1] = _arg4;
                this._targetDic[_arg1] = _arg3;
            } else {
                if (this._parentDic[_arg1] != null){
                    _arg2 = this._parentDic[_arg1];
                    _arg3 = this._targetDic[_arg1];
                } else {
                    return;
                };
            };
            this._hideDic[_arg1] = 0;
            if ((_arg1 is IFloatingPanel)){
                (_arg1 as IFloatingPanel).show(_arg3, _arg2);
            } else {
                _arg2.addChild((_arg1 as DisplayObject));
            };
        }
        public function hide(_arg1:DisplayObject):void{
            if (this._parentDic[_arg1] != null){
                this._hideDic[_arg1] = getTimer();
            };
        }
        private function onTimer(_arg1:TimerEvent):void{
            var _local3:DisplayObject;
            var _local4:Object;
            var _local2:int = getTimer();
            for (_local4 in this._delayDic) {
                if (((!((this._hideDic[_local4] == 0))) && (((_local2 - this._hideDic[_local4]) > this._delayDic[_local4])))){
                    _local3 = (_local4 as DisplayObject);
                    if ((((_local3 is IFloatingPanel)) && (((_local3 as IFloatingPanel).invalid == false)))){
                        (_local3 as IFloatingPanel).hide();
                    } else {
                        if ((this._parentDic[_local4] as DisplayObjectContainer).contains(_local3)){
                            (this._parentDic[_local4] as DisplayObjectContainer).removeChild(_local3);
                        };
                        delete this._parentDic[_local4];
                        delete this._delayDic[_local4];
                        delete this._hideDic[_local4];
                        delete this._targetDic[_local4];
                        if (ObjectPool.hasObject(_local4)){
                            ObjectPool.idle(_local4);
                        };
                    };
                };
            };
        }
        public function binding(_arg1, _arg2:Sprite, _arg3:DisplayObjectContainer, _arg4:int=1000):void{
            if (this._panelDic[_arg2] == null){
                this._panelDic[_arg2] = _arg1;
                this._panelParentDic[_arg2] = _arg3;
                this._panelTimeDic[_arg2] = _arg4;
                _arg2.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler, false, 0, true);
                _arg2.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOutHandler, false, 0, true);
            };
        }
        private function mouseOverHandler(_arg1:MouseEvent):void{
            var _local4:IFloatingPanel;
            var _local2:Object = _arg1.currentTarget;
            var _local3:* = this._panelDic[_local2];
            if (_local3 == null){
                return;
            };
            if ((_local3 is Class)){
                _local4 = (ObjectPool.getObjectInstance(_local3) as IFloatingPanel);
                ObjectPool.setMaxInstanceCount(_local3, 1);
            } else {
                _local4 = (_local3 as IFloatingPanel);
            };
            this._panelInstanceDic[_local2] = _local4;
            if (this._panelEventDic[_local4] != true){
                this._panelEventDic[_local4] = true;
                (_local4 as Sprite).addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler2, false, 0, true);
                (_local4 as Sprite).addEventListener(MouseEvent.MOUSE_OUT, this.mouseOutHandler2, false, 0, true);
            };
            this.show(_local4, this._panelParentDic[_local2], _local2, this._panelTimeDic[_local2]);
        }
        private function mouseOutHandler(_arg1:MouseEvent):void{
            var _local2:Object = _arg1.currentTarget;
            var _local3:* = this._panelInstanceDic[_local2];
            if (_local3 == null){
                return;
            };
            this.hide(_local3);
        }
        private function mouseOverHandler2(_arg1:MouseEvent):void{
            this.show(_arg1.currentTarget, null);
        }
        private function mouseOutHandler2(_arg1:MouseEvent):void{
            this.hide((_arg1.currentTarget as DisplayObject));
        }

    }
} 
